package org.jash.shop2112.utils

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.jash.shop2112.BuildConfig
import org.jash.shop2112.entity.User
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

var user: User? = null
private val client by lazy {
    OkHttpClient.Builder()
        .callTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .apply {
            if (BuildConfig.DEBUG) {
                addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            }
        }
        .addInterceptor {
            it.proceed(user?.let { u -> it.request().newBuilder().addHeader("token", u.token).build() } ?: it.request())
        }
        .build()
}
val retrofit by lazy {
    Retrofit.Builder()
        .baseUrl("http://10.161.9.80:7012/")
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}